<?php
/**
 *  Create On 2014-12-28
 *  Author yiwei
 *  QQ:1006629314
 **/
//error_reporting(0);
class PrepareController extends Controller{
 
	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/clientapi.php";
	}
	
	public function addPrepare(){
		$userinfo = $this->getData ( 'userinfo' );
		$user_id = $userinfo ['id'];
		/*
		$old_item_id_str = '23_34_12_31_25';
		$item_id_str = '1_2_23_322_31';
		
		$old_item_list = explode('_', $old_item_id_str);
		$item_list = explode('_', $item_id_str);
		$new_item_list = array_merge($old_item_list,array_diff($item_list,$old_item_list));
		var_dump($old_item_list);
		var_dump($item_list);
		var_dump($new_item_list);
		$new_item_num = count($new_item_list);
		$new_item_id_str = implode('_', $new_item_list);
		echo $new_item_id_str;
		*/
		if($_POST){
			$prepare = new prepare();
			$json = $_POST;
			$mode = $json['mode'];
			$item_id_str = $json['item_list'];
			$item_num = $json['item_num'];
			
			if($mode == 1){//批量新建
				$target_list = json_decode($json['target_list'],true);
				if($target_list){
					foreach ($target_list as $key=>$val){
						$check = $prepare->getPrepareByPeriod($val['period_id']);
						if($check){
							$old_item_id_str = $check['item_id_str'];
							$old_item_list = explode('_', $old_item_id_str);
							$item_list = explode('_', $item_id_str);
							$new_item_list = array_merge($old_item_list,array_diff($item_list,$old_item_list));
							$new_item_num = count($new_item_list);
							$new_item_id_str = implode('_', $new_item_list);
							$target_list[$key]['result'] = $prepare->modifyPrepare(
									$check['id'],$new_item_num, $new_item_id_str);
						}else{
							$target_list[$key]['result'] = $prepare->addPrepare($item_num ,$item_id_str, $val['period_id']);
						}
					}
				}
				$data = array("target_list"=>$target_list);
			}else{//选择修改
				$prepare_id = $json['prepare_id'];
				$result = $prepare->modifyPrepare($prepare_id, $item_num, $item_id_str);
				$data = array("prepare_id"=>$prepare_id,"result"=>$result);
			}
			$this->view->setData ( $data );
			$this->view->setState ( "1" );
			$this->view->setMsg ( "提交成功~" );
		}else{
			$this->view->setData ( (object)null );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "参数错误" );
		}
		$this->view->display ( "json" );
	}
	
	public function getPrepare(){
		$id = $this->getRequest()->get('id');
		$item = new item();
		$prepare = new prepare();
		$period = new period();
		$class_student = new class_student();
		$result = $prepare->getPrepare($id);
		if($result){
			$item_list = $item->getItemListByStr(1, $result['item_id_str']);
			$item_num = $result['item_num'];
			$periodInfo = $period->getPeriodById($result['period_id']);
			$studentList = $class_student->getStudentNameByPeriod($result['class_id']);
			$studentName = implode(' ', $studentList);
			$target_list = array("period_id"=>$result['period_id'],
								"className"=>$periodInfo['className'],
								"section"=>$periodInfo['section'],
								"course"=>$periodInfo['course'],
								"studentName"=>$studentName);
			$data = array("item_list"=>$item_list,"item_num"=>$item_num,"target_list"=>$target_list);	
			$this->view->setData ( $data );
			$this->view->setState ( "1" );
			$this->view->setMsg ( "获取成功~" );
		}else{
			$this->view->setData ( (object)null );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "备课不存在" );
		}
		$this->view->display ( "json" );
	}
	
	public function delPrepare(){
		$id = $this->getRequest()->get('id');
		$prepare = new prepare();
		if($id){
			$result = $prepare->delPrepare($id);
			if($result){
				$this->view->setData ( (object)null );
				$this->view->setState ( "1" );
				$this->view->setMsg ( "删除成功~" );
			}else{
				$this->view->setData ( (object)null );
				$this->view->setState ( "0" );
				$this->view->setMsg ( "删除失败" );
			}
		}else{
			$this->view->setData ( (object)null );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "参数缺失" );
		}
		$this->view->display ( "json" );		
	}
	
	public function getPrepareItemList(){
		$period_id = $this->getRequest()->get('periodId');
		//$period_id = 16;
		if($period_id){
			$prepare = new prepare();
			$item = new item();
			$result = $prepare->getPrepareByPeriod($period_id);
			$item_id_str = $result['item_id_str'];
			$item_list = explode('_', $item_id_str);
			$qiniu_path=$this->getApp()->loadConf('qiniupath');
			$list = array();
			if($item_list[0]){
				foreach ($item_list as $val){
					//$list[$val] = $item->getItemWithPicById($val);
					$content = $item->getItemContentById($val)?$item->getItemContentById($val):'略';
				        //$url =  $qiniu_path["qiniu_path_image"].'20150105item'.$val.'.jpg';
					array_push($list,array("content"=>$content,"item_id"=>$val));
				}
				$data = array("list"=>$list,"listCount"=>count($list));
			}else{
				$data = array("list"=>(object)null,"listCount"=>0);
			}			
			$this->view->setData ( $data );
			$this->view->setState ( "1" );
			$this->view->setMsg ( "获取成功~" );
		}else{
			$this->view->setData ( (object)null );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "参数缺失" );
		}
		$this->view->display ( "json" );
	}
}
